import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gift_listing/components/toast.dart';
import 'package:gift_listing/global.dart';
import 'package:gift_listing/iconfont.dart';

class FormText extends StatefulWidget {
  FormText({
    Key key,
    @required this.maxLine,
    @required this.hintText,
    this.text,
    this.onSubmitted,
    this.onChanged,
    this.onTap,
    this.controller,
    this.password = false,
    this.copy = false,
    this.readOnly = false,
  }) : super(key: key);
  final int maxLine;
  final String hintText;
  final onSubmitted;
  final onChanged;
  final onTap;
  final controller;
  final String text;
  final bool password;
  final bool copy;
  final bool readOnly;

  @override
  State<StatefulWidget> createState() => _FormTextState(text);
}

class _FormTextState extends State<FormText> {
  String text;
  _FormTextState(this.text);

  bool _obscureText = false;
  TextEditingController _formTextController = TextEditingController();

  Widget clipboard(context) {
    return Container(
      padding: EdgeInsets.only(right: 10),
      child: GestureDetector(
        onTap: () {
          Clipboard.setData(ClipboardData(text: _formTextController.text));
          Toast.show(context, msg: '已复制到剪贴板', duration: 1);
        },
        child: Icon(IconFont.copy, color: Global.fontSecondColor),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    double maxTextWidth = MediaQuery.of(context).size.width;
    _formTextController.text = this.text;
    return Container(
      padding: EdgeInsets.fromLTRB(10, 10, 15, 11),
      decoration: BoxDecoration(
        color: Global.fontSecondColor.withOpacity(0.08),
        borderRadius: BorderRadius.all(Radius.circular(7)),
      ),
      child: Row(
        children: [
          widget.copy ? clipboard(context) : Container(),
          Expanded(
            child: ConstrainedBox(
              constraints: BoxConstraints(
                maxWidth: maxTextWidth,
              ),
              child: TextField(
                onTap: widget.onTap,
                readOnly: widget.readOnly,
                controller: _formTextController,
                obscureText: _obscureText && widget.maxLine == 1,
                maxLines: widget.maxLine,
                style: TextStyle(
                  color: Global.fontColor,
                  fontSize: 14,
                ),
                decoration: InputDecoration.collapsed(
                  hintText: widget.hintText,
                  hintStyle: TextStyle(
                    color: Global.fontSecondColor.withOpacity(0.5),
                    fontSize: 14,
                  ),
                ),
                onSubmitted: widget.onSubmitted,
                onChanged: (text) {
                  this.text = text;
                  if (widget.onChanged != null) widget.onChanged(text);
                },
              ),
            ),
          ),
          widget.password
              ? GestureDetector(
                  onTap: () {
                    setState(() => {_obscureText = !_obscureText});
                  },
                  child: Icon(
                    IconFont.eyeopen,
                    color: _obscureText
                        ? Global.fontSecondColor
                        : Global.fontColor,
                  ),
                )
              : Container()
        ],
      ),
    );
  }
}
